/*
生成技术服务pdf 
*/
public without sharing class  TechnicalServiceController {

    public WorkOrder__c workOrder{get;set;}

    public Id id{get;set;}
    //结果及建议
    public String resSugestion {get;set;}
    //情况描述
    public String resionDescrible {get;set;}
    //服务情况
    public List<String> serviceSituation {get;set;}
    //仪器名称
    public String deviceName {get;set;}
    //仪器编号
    public String deviceNo {get;set;}
    //开始时间
    public String startTime {get;set;}
    //结束时间
    public String endTime {get;set;}
    //回访时间
    public String feadBackTime{get;private set;}
    //科室负责人
    public String responsePerson {get;set;}
    //科室负责人电话
    public String responsePhone {get;set;}
    ///客户名名称
    public String accName {get;set;}
    ///不良事件描述
    public String badDescribe{get;private set;}

    public Boolean isBad {get; private set;}

    public Boolean isTrack {get ; private set;}//是否追踪记录类型

    public String feedbackNote {get;private set;}

    public List<String> infoDes {get; private set;}

    //客户签名
    public Attachment accountSign {get;set;}    

    public TechnicalServiceController(ApexPages.StandardController con) {
        id=con.getId();
        workOrder = [  SELECT 
                     //工单
                     w.Name,w.Id,w.RecordType.DeveloperName,w.SystemUsage__c,ProjectResults__c,
                     //科室负责人
                     w.ManagementDepContact__r.Name,w.ManagementDepContact__r.MobilePhone,
                     //联系人
                     w.Contact__r.Name,w.Contact__r.MobilePhone,w.ReturninContact__c   ,
                     //客户
                     w.Account__c,w.Account__r.Name,w.Account__r.Province__r.Name,
                     //产品
                     w.ProductType2__c,w.ServiceManufacture__r.Product__r.Name ,w.ServiceManufacture__r.IMEINumber__c, 
                     //结果
                     w.ResultsSuggestions__c,w.ServiceSituation__c,ExecutionInformationDescription__c,
                     //服务类型
                     w.RecordType.Name,w.ServiceType__c,w.ServiceTrackingNotes__c,
                     //情况描述
                     w.Reason_Desc__c,w.AdverseEventDescription__c,AdverseEvent__c,
                     //时间
                     w.Install_Start_Datetime__c,w.Install_End_Datetime__c,w.ServicesTrackingTime__c,
                     //创建人
                     w.CreatedBy.Name ,w.CreatedBy.MobilePhone  

                     FROM WorkOrder__c w 
                     WHERE w.Id = :id ];


        if(null!=workOrder){

            /*  List<Contact> contact= [  SELECT Name,MobilePhone 
                                            FROM Contact c
                                            WHERE c.Account.Id = : workOrder.Account__c and 
                                            c.Departments__c = '检验科' and c.Post__c = '科长/主任' and
                                            c.PositionSituation__c = '在职'
                                             ];
            for(Contact c : contact){
                 responsePerson = c.Name;
                 responsePhone = c.MobilePhone;
                 break;    
             }
            */
             isTrack  = workOrder.RecordType.DeveloperName == 'RecordTypeTrackingRecordAfterTraining'; 
            List<Attachment> attachment= [select Body,Name,CreatedDate,Id from Attachment where ParentId = :id];

            for(Attachment att : attachment){
                if (att.Name=='使用科室负责人_Sign.jpg'){
                    accountSign=att;
                }else if( att.Name=='管理科室负责人_Sign.jpg'){
                    //manageSign=att;
                }else if( att.Name == '工程师_Sign.jpg'){
                    //engineerSign = att;
                }
            }  
            responsePerson = workOrder.ManagementDepContact__r.Name;
            responsePhone = workOrder.ManagementDepContact__r.MobilePhone;

            if(workOrder.Install_Start_Datetime__c != null){
                startTime = workOrder.Install_Start_Datetime__c.format('yyyy年MM月dd日');
            }
            if(workOrder.Install_End_Datetime__c != null){
                endTime = workOrder.Install_End_Datetime__c.format('yyyy年MM月dd日');
            }
            if(workOrder.ServicesTrackingTime__c != null ){
                feadBackTime = workOrder.ServicesTrackingTime__c.format('yyyy年MM月dd日');
            }
            isBad = workOrder.AdverseEvent__c == '有不良事件' ? true : false ;
            resSugestion = kaigyo(workOrder.ResultsSuggestions__c , 50);
            serviceSituation  =  autoNextLine(workOrder.ServiceSituation__c , 50);
            resionDescrible = kaigyo(workOrder.Reason_Desc__c,50);
            deviceName = kaigyo(workOrder.ServiceManufacture__r.Product__r.Name ,50);
            badDescribe =  kaigyo(workOrder.AdverseEventDescription__c,50);
             accName = kaigyo(workOrder.Account__r.Name, 50);
             feedbackNote = kaigyo(workOrder.ServiceTrackingNotes__c, 58);
             infoDes = autoNextLine(workOrder.ExecutionInformationDescription__c, 50);



        }


    }
   
    public void init(){
        WorkOrder__c wo = new WorkOrder__c(
            Id = workOrder.Id,
            GenerateTechnicalServiceReportTime__c = System.now()
        );

       update wo;
    }
    //计算文本长度自动换行 value字符长度 posLength换行字数
    private String kaigyo(String value, Integer posLength ){
        if(value !=null)value = value.replaceAll('\r\n','');
        if(value == null || value.length() <= posLength) return value;

        String rtnValue = value.substring(0, posLength ) + '\n';

        for(Integer i = posLength; i < value.length(); i++){
            if(Math.mod(i, posLength ) == 0){
                if(value.length() > i + posLength){
                    rtnValue += value.substring(i, i + posLength ) + '\n'; 
                }else{ 
                    rtnValue += value.substring(i,value.length());
                } 
                // kaigyoCount[col]++;
            }
        }
        return rtnValue;
    }
    private List<String> autoNextLine(String value, Integer posLength ){
        List <String> temp = new List<String>();
        if(value == null ) 
        {
            temp.add(value);
            return temp; 
        }
        List<String> str = new List<String>();
        str =  value.split('\r\n',-1);
        for (String s :str){

            if(s.length() > posLength){
                for(Integer i = 0; i < s.length(); i++){
                    if(Math.mod(i, posLength ) == 0){
                        if(s.length() > i + posLength){
                            temp.add(s.substring(i, i + posLength)); 
                        }else{ 
                            temp.add(s.substring(i,s.length()));
                        } 

                        // kaigyoCount[col]++;
                    }
                }

            }else{
                temp.add(s);

            }
        }

        return temp;
    }

}